package com.scholarship.modules.admin.controller;

import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.common.utils.FileUploadUtil;
import com.scholarship.common.utils.NewFileUtil;
import com.scholarship.modules.admin.dto.StudentQueryDto;
import com.scholarship.modules.admin.exce.ImportFailException;
import com.scholarship.modules.admin.service.InOutFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件导入导出控制层
 * @Author Z.J.H
 * @create 2021/9/9 21:02
 */
@Slf4j
@Api(tags = {"文件导入导出控制层"})
@RestController
public class InOutFileController {

    @Autowired
    private InOutFileService inOutFileService;

    @Autowired
    private NewFileUtil newFileUtil;

    /**
     * 下载指定导入模板
     * @return
     * @throws Exception
     */
    @ApiOperation("下载指定导入模板")
    @GetMapping("/admin/download/file")
    public void downloadImportTemplateFile(@RequestParam("tableId") Integer tableId,
                                           HttpServletResponse response) throws Exception {
        try {
            inOutFileService.downLoadImportTemplate(tableId, response);
            //inOutFileService.downLoadImportTemplateNew(tableId,response);
        }catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 下载导入模板
     * @return
     * @throws Exception
     */
    @ApiOperation("下载导入模板zip")
    @GetMapping("/admin/download/zip")
    public void downloadImportTemplateZip(HttpServletResponse response) throws Exception {
        try {
            inOutFileService.downLoadImportTemplateZip(response);
        }catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 下载学生信息列表
     * @return
     * @throws Exception
     */
    @ApiOperation("下载学生信息列表")
    @GetMapping("/admin/download/student")
    public void downloadStudentInfo(StudentQueryDto studentQueryDto, HttpServletResponse response) throws Exception {
        try {
            inOutFileService.downLoadStudentInfo(studentQueryDto, response);
        }catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 导入学生信息
     * @param file  excel文件
     * @return
     * @throws IOException
     */
    @ApiOperation("导入学生基本信息")
    @PostMapping("/admin/import/student")
    public RetResult importbasicUserInfo(@RequestParam MultipartFile file) throws IOException {
        try {
            if(!file.isEmpty()) {
                String fileName = newFileUtil.uploadFile(file);
                inOutFileService.importStudentTemplate(fileName);
                return RetResponse.makeOKRsp();
            }else {
                return RetResponse.makeRsp(RetCode.EMPTY_CONTENT, "文件不存在");
            }
        } catch(OLE2NotOfficeXmlFileException e) {
            return RetResponse.makeRsp(RetCode.IMPORT_FAIL,"请将文件另存为xlsx文件再导入");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"文件导入失败");
        }
    }

    /**
     * 导入四六级
     * @param file  excel文件
     * @return
     * @throws IOException
     */
    @ApiOperation("导入四六级")
    @PostMapping("/admin/import/cet")
    public RetResult importCET4AndCET6(@RequestParam MultipartFile file) throws IOException {
        try {
            if(!file.isEmpty()) {
                String fileName = newFileUtil.uploadFile(file);
                inOutFileService.importCetTemplate(fileName);
                return RetResponse.makeOKRsp();
            }else {
                return RetResponse.makeRsp(RetCode.EMPTY_CONTENT, "文件不存在");
            }
        } catch(OLE2NotOfficeXmlFileException e) {
            return RetResponse.makeRsp(RetCode.IMPORT_FAIL,"请将文件另存为xlsx文件再导入");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"文件导入失败");
        }
    }

    /**
     * 导入通用奖项模板
     * @param file  excel文件
     * @return
     * @throws IOException
     */
    @ApiOperation("导入通用奖项模板(admin,backend)")
    @PostMapping(value = {"/admin/import/common", "/backend/import/common"})
    public RetResult importCommonTemplate(@RequestParam MultipartFile file) throws IOException {
        try {
            if(!file.isEmpty()) {
                String fileName = newFileUtil.uploadFile(file);
                inOutFileService.importCommonTemplate(fileName);
                return RetResponse.makeOKRsp();
            }else {
                return RetResponse.makeRsp(RetCode.EMPTY_CONTENT, "文件不存在");
            }
        }catch(OLE2NotOfficeXmlFileException e) {
            return RetResponse.makeRsp(RetCode.IMPORT_FAIL,"请将文件另存为xlsx文件再导入");
        } catch (ImportFailException e) {
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RetResponse.makeRsp(RetCode.FAIL,"文件导入失败");
        }
    }
}
